﻿using Inovout.Kuajing.Models;
using Inovout.Kuajing.Services;
using Inovout.Kuajing.Store.Models;
using Inovout.Kuajing.Store.Services;
using Inovout.Kuajing.Store.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;

namespace Inovout.Kuajing.Store.Web.Controllers
{
    public class OrderController : KJBaseController<ShipmentOrder>
    {
        private ShipmentOrderItemService shipmentOrderItemService;
        private ShipmentOrderService shipmentOrderService;
        private ConsignerService consignerService;
        private CVSService cvsService = new CVSService();
        public OrderController(ShipmentOrderService shipmentOrderService, ConsignerService consignerService, ShipmentOrderItemService shipmentOrderItemService)
        {
            this.shipmentOrderService = shipmentOrderService;
            this.consignerService = consignerService;
            this.shipmentOrderItemService = shipmentOrderItemService;
        }


        public ActionResult UnShippedOrderList()
        {
            int consignerId = Consigner.Id;
            IList<ShipmentOrder> shipmentOrderList = shipmentOrderService.GetByConsigner(consignerId.ToString());
            // int shipperId= int.Parse(consignerId.ToString().Substring(1, 2));
            // IList<ShipmentOrder> shipmentOrderList = shipmentOrderService.GetByShipperId(shipperId);
            return View(shipmentOrderList);
        }

        public ActionResult ShippedOrderList()
        {
            int consignerId = Consigner.Id;
            //int shipperId = int.Parse(consignerId.ToString().Substring(1, 2));
            IList<ShipmentOrder> shipmentOrderList = shipmentOrderService.GetShippedOrderByConsigner(consignerId.ToString());
            //IList<ShipmentOrder> shipmentOrderList = shipmentOrderService.GetShippedOrderByShipperId(shipperId);
            return View(shipmentOrderList);
        }


        [HttpPost]
        public ActionResult SaveSingleUnShippedOrder(string orderId, string logisticsOrderNo, string logisticsCompanyName)
        {
            ShipmentOrder shipmentOrder = shipmentOrderService.FindById(int.Parse(orderId));
            shipmentOrder.LogisticsOrderNo = logisticsOrderNo;
            shipmentOrder.Status = ShipmentOrderStatus.Shipped;
            shipmentOrder.LogisticsCompanyName = logisticsCompanyName;
            shipmentOrderService.Save(shipmentOrder);
            return RedirectToAction("UnShippedOrderList");
        }

        [HttpPost]
        public ActionResult SaveSingleShippedOrder(string orderId, string logisticsOrderNo, string logisticsCompanyName)
        {
            ShipmentOrder shipmentOrder = shipmentOrderService.FindById(int.Parse(orderId));
            shipmentOrder.LogisticsOrderNo = logisticsOrderNo;
            shipmentOrder.Status = ShipmentOrderStatus.Shipped;
            shipmentOrder.LogisticsCompanyName = logisticsCompanyName;
            shipmentOrderService.Save(shipmentOrder);
            return RedirectToAction("ShippedOrderList");
        }

        public ActionResult ExportOrderItems()
        {
            int consignerId = Consigner.Id;
            Consigner consigner = consignerService.FindById(consignerId);
            return View(consigner);
        }

        [HttpPost]
        public FilePathResult DownLoadOrderItems(Consigner consigner, string templateHeader, string IsTab)
        {
            //保存csv模板
            consignerService.Save(consigner);
            var headers = templateHeader;
            //下载订单项
            string filename = consigner.OrganizationUnit.Name + DateTime.Now.ToFileTime().ToString() + ".csv";
            string outputPath = Server.MapPath("/Files/Json/" + filename);
            int id = consigner.Id;
            cvsService.WriteCsvFile(consigner.Template.Split(','), outputPath, id.ToString(), consigner.IsTab, headers);

            return File(outputPath, "text/csv", filename);
        }

        [HttpGet]
        public FilePathResult DownLoadLogistisc()
        {
            //下载模板
            string filename = "LogisticTemplate.csv";
            string outputPath = Server.MapPath("/Files/CSV/" + filename);


            return File(outputPath, "text/csv", filename);
        }

        [HttpGet]
        public ActionResult UploadOrders()
        {
            return View(this.TempData["listShipmentOrder"]);
        }

        [HttpPost]
        public ActionResult UploadOrder()
        {

            string filePath = Server.MapPath("/Files/CSV");
            string fileName = DateTime.Now.ToFileTime().ToString();
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase fileUpload = Request.Files[i];
                fileUpload.SaveAs(filePath + fileName);
            }
            int consignerId = Consigner.Id;
            IList<ShipmentOrder> listShipmentOrder = cvsService.ReadCsvFile(filePath + fileName, consignerId.ToString());
            this.TempData["listShipmentOrder"] = listShipmentOrder;
            return RedirectToAction("UploadOrders");
        }

        [HttpPost]
        public ActionResult SaveOrder(string orderNosLogisticsOrderNos)
        {
            List<OrderInfo> orderInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<OrderInfo>>(orderNosLogisticsOrderNos);
            foreach (OrderInfo item in orderInfoList)
            {
                ShipmentOrder shipmentOrder = shipmentOrderService.GetByNo(item.OrderNo);
                shipmentOrder.LogisticsOrderNo = item.LogisticsOrderNo;
                shipmentOrder.Status = ShipmentOrderStatus.Shipped;
                shipmentOrder.LogisticsCompanyCode = item.LogisticsCode;
                shipmentOrder.LogisticsCompanyName = item.LogisticsName;
                shipmentOrderService.Save(shipmentOrder);
            }
            return RedirectToAction("UnShippedOrderList");
        }

        [HttpGet]
        public ActionResult LoadOrderDetail(string orderNo, bool is_ship)
        {
            int consignerId = Consigner.Id;
            IList<ShipmentOrderItem> shipmentOrderItemList = shipmentOrderItemService.GetByOrderNoAndConsigner(consignerId.ToString(), orderNo);
            ViewBag.Ship = is_ship;
            return View(shipmentOrderItemList);
        }

    }
}